/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.jQueryForm.Ddl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/*
 * Json Format
 * {"ddl" :    [ #1
 *                      { #2
 *                          "id" :         "#ddlSelector",
 *                          "defaultType" :       "form" Or "search" Or Nothing,
 *                          "value" :      {
 *                                           "Value 1" : "Text 1",
 *                                           "Value 2" : "Text 2",
 *                                           "Value 3" : "Text 3"
 *                                         },
 *                          "selectValue" :       "value to select"
 *                      },
 *                      {
 *                          .
 *                          .
 *                          .
 *                      }
 *                      .
 *                      .
 *                      .
 *             ]
 * }
 */
public class jQueryFormDdl {

    private String id;
    private List<Map<String, String>> value;
    private String selectValue;

    public jQueryFormDdl() {
    }

    public jQueryFormDdl(String id, String selectValue, List<? extends Object> optItem, String optionMethodValue, String optionMethodName) throws Exception {
        this.id = id;
        this.selectValue = selectValue;
        this.setValue(optItem, optionMethodValue, optionMethodName);
    }
	
	public jQueryFormDdl(String id, String selectValue) throws Exception {
        this.id = id;
        this.selectValue = selectValue;
    }

    public String getId() {
        return id;
    }

    private void setId(String id) {
        this.id = id;
    }

    public String getSelectValue() {
        return selectValue;
    }

    private void setSelectValue(String selectValue) {
        this.selectValue = selectValue;
    }

    public List<Map<String, String>> getValue() {
        return this.value;
    }

    public void setValue(List<Map<String, String>> objValue) throws Exception {

        this.value = objValue;
    }
	
	private void setValue(List<? extends Object> lOption, String optionMethodValue, String optionMethodName) throws Exception {
        Map<String, String> lOptValue = null;
        List<Map<String, String>> lValue = new ArrayList<Map<String, String>>();
        Class partypes[] = new Class[0];// No parameter!!!
        Object arglist[] = new Object[0];
        try {
            for (Object objOption : lOption) {
                lOptValue = new LinkedHashMap<String, String>();
                Method methOptionValue = objOption.getClass().getMethod(
                        optionMethodValue, partypes);
                String optionValue = methOptionValue.invoke(objOption, arglist).toString();
                Method methOptionName = objOption.getClass().getMethod(
                        optionMethodName, partypes);
                String optionName = methOptionName.invoke(objOption, arglist).toString();
                lOptValue.put(optionValue, optionName);
                lValue.add(lOptValue);
            }
        } catch (Exception e) {
            System.err.println(e);
            throw e;
        }


        this.value = lValue;
    }
}
